<!DOCTYPE html>
<html lang="zh-CN"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!--<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />-->
    <link rel="stylesheet" data-th-href="@{/css/font.css}">
    <link rel="stylesheet" data-th-href="@{/css/xadmin.css}">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" data-th-src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" data-th-src="@{/js/xadmin.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form" id="save_form" data-th-action="@{/site/saveImg}">
        <div class="layui-form-item">
            <label for="ImgUrl" class="layui-form-label">
                <span class="x-red">*</span>图片
            </label>
            <div class="layui-input-block">

                <div data-th-if="${#strings.isEmpty(ImgUrl)}" id="photoFrame" style='background-color: #9F9F9F;display:none; margin-bottom:10px;width: 310px;height: 190px;'>
                    <img id="upload_img" style='height: 180px;width: 300px;margin: 5px 5px 5px 5px;' src="">
                </div>

                <div data-th-if="${!#strings.isEmpty(ImgUrl)}" id="photoFrame" style='background-color: #9F9F9F;display:block; margin-bottom:10px;width: 310px;height: 190px;'>
                    <img id="upload_img" style='height: 180px;width: 300px;margin: 5px 5px 5px 5px;' data-th-src="${ImgUrl}">
                </div>

                <button type="button" class="layui-btn" id="upload">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
                <span style="color: #e60000">（建议图片尺寸600*360）</span>
                <input type="hidden" id="ImgUrl" name="ImgUrl" required="" lay-verify="required" data-th-value="${ImgUrl}"
                       autocomplete="off" class="layui-input">
            </div>

        </div>
        <div class="layui-form-item">
            <button  class="layui-btn" lay-filter="add" lay-submit="add">立即提交</button>
        </div>
    </form>
</div>
<script>
    layui.use(['form','layer','upload'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer
            ,upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,url: '/file/fileUpload' //上传接口
            ,before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                layer.load(); //上传loading
            }
            ,done: function(res){
                //上传完毕回调
                console.log(res); //上传成功返回值，必须为json格式
                console.log("上传完成");
                $("#ImgUrl").val(res.path);
                $("#upload_img").prop("src",res.path);
                $("#photoFrame").show();
                top.layer.msg(res.msg);
                layer.closeAll('loading'); //关闭loading
            }
            ,error: function(){
                //请求异常回调
            }
        });



        form.on("submit(add)",function(data){
            var index = layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            var url = $("#save_form").attr("action");
            var data = $("#save_form").serializeArray(); //自动将form表单封装成json
            //console.log(url);
            setTimeout(function(){
                $.ajax({
                    type:"post",
                    url:url,
                    data:data,
                    dataType:"json",
                    success:function(result){
                        layer.close(index);
                        //console.log(result.message);
                        layer.msg(result.message);
                        //刷新父页面
//							console.log(window.location);
                    }
                });
                layer.close(index);
            },1000);
            return false;
        })

    });
</script>
</body>
</html>